package org.hitchhike.core.service;

import java.util.HashSet;
import java.util.Set;

import org.hitchhike.common.service.BaseService;
import org.hitchhike.core.domain.Order;
import org.hitchhike.core.domain.OrderDetail;
import org.hitchhike.core.enums.Constant;
import org.hitchhike.core.repository.OrderDetailRepository;
import org.hitchhike.core.utils.PageableUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class OrderDetailService extends BaseService<OrderDetail, Long> {
  private Logger logger = LoggerFactory.getLogger(getClass());

  @Autowired
  private OrderDetailRepository getOrderDetailRepository() {
    return (OrderDetailRepository) baseRepository;
  }

  @Autowired
  private OrderService orderService;

  public Page<OrderDetail> findPage(Integer page, Long userId, String state) {
    if (userId == null) {
      return null;
    }
    if (state == null) {
      return null;
    }

    Set<Integer> states = new HashSet<Integer>();
    if (state.equals("1")) {
      states.add(1);
    } else if (state.equals("02")) {
      states.add(0);
      states.add(2);
    } else {
      return null;
    }

    return getOrderDetailRepository().findPage(userId, states, PageableUtil.getPageable(page, 10));
  }


  /**
   * 检查车票是否取消
   * 
   * @return
   */
  public boolean checkCancelTicket(Long orderId) {
    int count = getOrderDetailRepository().checkCancelTicket(orderId);
    logger.info("车票还有" + count + "没取消。");
    if (count == 0) {
      return true;
    }
    return false;
  }

  /**
   * 检查车票是否付款
   * 
   * @return
   */
  public boolean checkPaymentTicket(Long orderId) {
    int count = getOrderDetailRepository().checkPaymentTicket(orderId);
    logger.info("车票还有" + count + "没付款。");
    if (count == 0) {
      return true;
    }
    return false;
  }

  /**
   * 修改订单明细-取消
   */
  @Transactional
  public void updateCancel(Long orderId) {
    getOrderDetailRepository().updateCancel(orderId);
  }

  /**
   * 修改订单明细-支付
   */
  @Transactional
  public void updatePayment(Long orderId) {
    getOrderDetailRepository().updatePayment(orderId);
  }


  @Transactional
  public void cancel(Long id) {
    OrderDetail orderDetail = super.get(id);
    orderDetail.setState(Constant.ORDER_DETAIL_STATE_0);
    super.update(orderDetail);

    if (this.checkCancelTicket(orderDetail.getOrder().getId())) {
      Order order = orderService.get(orderDetail.getOrder().getId());
      order.setOrderState(Constant.ORDER_STATE_1);
      order.setOrderProcess(Constant.ORDER_PROCESS_0);
      orderService.update(order);
    }
  }

  @Transactional
  public void confirm(Long id) {
    OrderDetail orderDetail = super.get(id);
    Order order = orderDetail.getOrder();
    order.setOrderState(Constant.ORDER_STATE_2);
    order.setOrderProcess(Constant.ORDER_PROCESS_2);
    orderService.update(order);
  }

  @Transactional
  public void payment(Long id) {
    OrderDetail orderDetail = super.get(id);
    orderDetail.setState(Constant.ORDER_DETAIL_STATE_2);
    super.update(orderDetail);

    if (this.checkPaymentTicket(orderDetail.getOrder().getId())) {
      Order order = orderService.get(orderDetail.getOrder().getId());
      order.setOrderState(Constant.ORDER_STATE_3);
      order.setOrderProcess(Constant.ORDER_PROCESS_3);
      orderService.update(order);
    }
  }

}
